<?php
$url = $_POST['url'];
$name = $_POST['name'];
$password = $_POST['password'];
$db = $_POST['db'];
$port = $_POST['port'];

$content = "<?php\n";
$content .= "\$link = mysqli_connect('$url','$name','$password','$db','$port');\n";
$content .= "error_reporting(0);\n";
$content .= "\$web_info = \"select * from webinfo\";\n";
$content .= "\$web_info_result = \$link->query(\$web_info);\n";
$content .= "\$web_info_row = mysqli_fetch_array(\$web_info_result);\n";
$content .= "?>\n";

$filePath = '../sql.php';

if (!file_exists($filePath)) {
    $fileHandle = fopen($filePath, 'w');
    if ($fileHandle) {
        fwrite($fileHandle, $content);
        fclose($fileHandle);
        echo "数据库连接成功！";
    } else {
        echo "无法创建数据库文件。";
    }
} else {
    echo "数据库文件已经存在。";
}

$createTablesSQL = "
SET SQL_MODE = \"NO_AUTO_VALUE_ON_ZERO\";
START TRANSACTION;
SET time_zone = \"+00:00\";

CREATE TABLE `admin_log` (
  `uid` int(11) NOT NULL,
  `ip` varchar(255) NOT NULL,
  `time` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `admin_user` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO `admin_user` (`id`, `name`, `password`) VALUES
(1, 'admin', '21232f297a57a5a743894a0e4a801fc3');

CREATE TABLE `article` (
  `id` int(11) NOT NULL,
  `title` varchar(255) NOT NULL,
  `text` text NOT NULL,
  `time` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `mlzyl` (
  `id` int(11) NOT NULL,
  `mlzyl` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO `mlzyl` (`id`, `mlzyl`) VALUES
(1, '哲学类'),
(2, '经济学类'),
(3, '财政学类'),
(4, '金融学类'),
(5, '经济与贸易类'),
(6, '法学类'),
(7, '政治学类'),
(8, '社会学类'),
(9, '民族学类'),
(10, '马克思主义理论类'),
(11, '公安学类'),
(12, '教育学类'),
(13, '体育学类'),
(14, '中国语言文学类'),
(15, '外国语言文学类'),
(16, '新闻传播学类'),
(17, '历史学类'),
(18, '数学类'),
(19, '物理学类'),
(20, '化学类'),
(21, '天文学类'),
(22, '地理科学类'),
(23, '大气科学类'),
(24, '海洋科学类'),
(25, '地球物理学类'),
(26, '地质学类'),
(27, '生物科学类'),
(28, '心理学类 '),
(29, '统计学类'),
(30, '力学类'),
(31, '机械类'),
(32, '仪器类'),
(33, '材料类'),
(34, '能源动力类'),
(35, '电气类'),
(36, '电子信息类'),
(37, '自动化类'),
(38, '计算机类'),
(39, '土木类'),
(40, '水利类'),
(41, '测绘类'),
(42, '化工与制药类'),
(43, '地质类'),
(44, '矿业类'),
(45, '纺织类'),
(46, '轻工类'),
(47, '交通运输类'),
(48, '海洋工程类'),
(49, '航空航天类'),
(50, '兵器类'),
(51, '核工程类'),
(52, '农业工程类'),
(53, '林业工程类'),
(54, '环境科学与工程类'),
(55, '生物医学工程类'),
(56, '食品科学与工程类'),
(57, '建筑类'),
(58, '安全科学与工程类'),
(59, '生物工程类'),
(60, '公安技术类'),
(61, '交叉工程类'),
(62, '植物生产类'),
(63, '自然保护与环境生态类'),
(64, '动物生产类'),
(65, '动物医学类'),
(66, '林学类'),
(67, '水产类'),
(68, '草学类'),
(69, '基础医学类'),
(70, '临床医学类'),
(71, '口腔医学类'),
(72, '公共卫生与预防医学类'),
(73, '中医学类'),
(74, '中西医结合类'),
(75, '药学类'),
(76, '中药学类'),
(77, '法医学类'),
(78, '医学技术类'),
(79, '护理学类'),
(80, '管理科学与工程类'),
(81, '工商管理类'),
(82, '农业经济管理类'),
(83, '公共管理类'),
(84, '图书情报与档案管理类'),
(85, '物流管理与工程类'),
(86, '工业工程类'),
(87, '电子商务类'),
(88, '旅游管理类'),
(89, '艺术学理论类'),
(90, '音乐与舞蹈学类'),
(91, '戏剧与影视学类'),
(92, '美术学类'),
(93, '设计学类');

CREATE TABLE `speciality` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL COMMENT '专业',
  `mlzyl` varchar(255) NOT NULL COMMENT '门类专业类',
  `xwsyml` varchar(255) NOT NULL COMMENT '学位授予门类',
  `years` int(11) NOT NULL COMMENT '学制',
  `school` varchar(255) NOT NULL COMMENT '培养院系',
  `total` int(11) NOT NULL COMMENT '招生人数',
  `price` int(11) NOT NULL COMMENT '学费/年'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `upload` (
  `id` int(11) NOT NULL,
  `uid` int(11) NOT NULL,
  `files` varchar(255) NOT NULL,
  `type` varchar(255) NOT NULL,
  `time` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `user` (
  `id` int(11) NOT NULL COMMENT '考生序号',
  `number` text NOT NULL COMMENT '考生身份证号',
  `password` varchar(255) NOT NULL COMMENT '登入密码',
  `name` varchar(255) NOT NULL COMMENT '真实姓名',
  `telephone` varchar(11) NOT NULL COMMENT '电话号码',
  `mail` varchar(255) NOT NULL COMMENT '邮箱',
  `ip` varchar(20) NOT NULL COMMENT '注册IP地址',
  `time` int(11) NOT NULL COMMENT '注册时间',
  `exnumber` int(11) NOT NULL COMMENT '准考证号',
  `photo` varchar(255) NOT NULL COMMENT '考生照片',
  `cardz` varchar(255) NOT NULL COMMENT '身份证正面',
  `cardf` varchar(255) NOT NULL COMMENT '身份证反面',
  `sqb` varchar(255) NOT NULL COMMENT '考生申请表',
  `cns` varchar(255) NOT NULL COMMENT '考生承诺书',
  `bgd` varchar(255) NOT NULL COMMENT '学历验证报告单',
  `cjd` varchar(255) NOT NULL COMMENT '本科成绩单',
  `address` text NOT NULL COMMENT '邮寄地址',
  `school` varchar(255) NOT NULL COMMENT '毕业院校',
  `bkzy` varchar(255) NOT NULL COMMENT '本科专业',
  `zylb` varchar(255) NOT NULL COMMENT '本科专业门类',
  `years` varchar(255) NOT NULL COMMENT '毕业年份',
  `xlzs` varchar(255) NOT NULL COMMENT '学历证书',
  `xwzs` varchar(255) NOT NULL COMMENT '学位证书',
  `xlzsphoto` varchar(255) NOT NULL COMMENT '学历证书照片',
  `xwzsphoto` varchar(255) NOT NULL COMMENT '学位证书照片',
  `speciality` varchar(255) NOT NULL COMMENT '报考专业',
  `first` int(11) NOT NULL COMMENT '资格审查',
  `why` text NOT NULL COMMENT '未通过原因',
  `second` int(11) NOT NULL COMMENT '参与考试'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `user_log` (
  `uid` int(11) NOT NULL,
  `ip` varchar(255) NOT NULL,
  `time` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `webinfo` (
  `id` int(11) NOT NULL,
  `webname` varchar(255) NOT NULL,
  `tel` varchar(255) NOT NULL,
  `mail` varchar(255) NOT NULL,
  `address` varchar(255) NOT NULL,
  `reg` int(11) NOT NULL,
  `icp` varchar(255) NOT NULL,
  `banner` varchar(255) NOT NULL,
  `introduce` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `xw` (
  `id` int(11) NOT NULL,
  `xw` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO `xw` (`id`, `xw`) VALUES
(1, '哲学'),
(2, '经济学'),
(3, '法学'),
(4, '文学'),
(5, '历史学'),
(6, '理学'),
(7, '工学'),
(8, '医学'),
(9, '农学'),
(10, '管理学'),
(11, '教育学'),
(12, '艺术学');

ALTER TABLE `admin_user`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `article`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `mlzyl`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `speciality`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `upload`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `user`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `webinfo`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `xw`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `admin_user`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;

ALTER TABLE `article`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `mlzyl`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=94;

ALTER TABLE `speciality`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `upload`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `user`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '考生序号';

ALTER TABLE `webinfo`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `xw`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=13;
COMMIT;
";

$conn = mysqli_connect($url, $name, $password, $db, $port);

// 使用新创建的数据库连接对象执行 SQL 查询
if (mysqli_multi_query($conn, $createTablesSQL)) {
    do {
        if ($result = mysqli_store_result($conn)) {
            mysqli_free_result($result);
        }
    } while (mysqli_more_results($conn) && mysqli_next_result($conn));
    echo "数据表创建成功！请删除根目录下 /install/ 文件夹。";
} else {
    echo "数据表创建失败：" . mysqli_error($conn);
}

// 关闭数据库连接
mysqli_close($conn);
?>